<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library.Database.Adapter
 */
class Dmf_Database_Adapter_Mysqlbasic extends Dmf_Database_Adapter_Abstract
{
    /**
     * The mysql connection resource
     * 
     * @var resource 
     */
    private $_connection = null;
    
    /**
     * Get mysql connection
     * 
     * @return resource
     * @throws Dmf_Exception_Mysql 
     */
    public function getConnection()
    {
        if ( $this->_connection == null )
        {
            $this->_connection = @mysql_connect( $this->_settings['server'], $this->_settings['username'], $this->_settings['password'] );
            $db = @mysql_select_db( $this->_settings['database'], $this->_connection );
            
            if ( ! $db || ! $this->_connection )
            {
                throw new Dmf_Exception_Mysql( 'Could not connect to mysql server.' );
            }
        }
        
        return $this->_connection;
    }
    
    /**
     * Execute query
     * 
     * @param string $query
     * @return resource
     * @throws Dmf_Exception_Mysql 
     */
    public function query( $query )
    {
        //Create DB-connection because mysql_real_escape_string needs it
        $con = $this->getConnection();
        $parameters = func_get_args();
        
        //Query is actually parameter 0
        unset( $parameters['0'] );
        
        //Index is 1 because indexes aren't changed since unsettings first
        if ( isset( $parameters['1'] ) && is_array( $parameters['1'] ) )
        {
            $parameters = $parameters['1'];
        }
        
        $parameters = array_values( $parameters );
        $querySplit = explode( '?', $query );
        $query = '';
        $i = 0;
        
        foreach( $querySplit as $queryPart )
        {
            if ( isset( $parameters[ $i ] ) )
            {
                $query .= $queryPart . "'" . mysql_real_escape_string( $parameters[ $i ] ) . "'";
            }
            else
            {
                $query .= $queryPart;
            }
            
            $i++;
        }
        
        $q = @mysql_query( $query, $this->getConnection() );
        Dmf_Database_Adapter::$queries_executed++;
        
        if ( ! $q )
        {
            throw new Dmf_Exception_Mysql( mysql_error() );
        }
        
        return $q;
    }
    
    /**
     * Fetch all rows
     * 
     * @param string $query
     * @return array
     */
    public function fetchAll( $query )
    {
        $array = array();
        $query = $this->query( $query );
        
        while( $row = mysql_fetch_assoc( $query ) )
        {
            $array[] = $row;
        }
        
        return $array;
    }
    
    /**
     * Fetch row
     * 
     * @param string $query
     * @return array
     */
    public function fetchRow( $query )
    {
        $array = $this->fetchAll( $query );
        
        return $array['0'];
    }
    
    /**
     * Get mysql result
     * 
     * @param string $query
     * @return boolean
     */
    public function result( $query )
    {
        return (boolean)@mysql_result( $this->query( $query ) );
    }
    
    /**
     * Count rows
     * 
     * @param string $query
     * @return int
     */
    public function rowCount( $query )
    {
        return @mysql_numrows( $this->query( $query ) );
    }
}